﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyHealthByDesign.Domain.Panels
{
    public class MultipleTextBoxes : IQuestionPanel
    {
        #region IQuestionPanel Members

        public System.Web.UI.WebControls.Panel GenerateQuestionDisplay(Question q)
        {
            Panel pnlIn = new Panel();
            
            Label lblC = new Label();
            lblC.Text = q.ContentLabel;
            lblC.CssClass = "panelLeft";

            pnlIn.Controls.Add(lblC);

            List<string> FieldNames = new List<string>();
            List<string> FieldSuffixes = new List<string>();
            List<string> FieldCSSs = new List<string>();

            // load Generic Lists
            FieldNames = new List<string>(q.Name.Split('|'));

            if(!string.IsNullOrEmpty(q.FieldCSS))
                FieldCSSs =  new List<string>(q.FieldCSS.Split('|'));

            if (!string.IsNullOrEmpty(q.FieldSuffix))
                FieldSuffixes =  new List<string>(q.FieldSuffix.Split('|'));

            for(int i = 0; i < FieldNames.Count; i++ )
            {
                Type typField = typeof(TextBox).Assembly.GetType("System.Web.UI.WebControls.TextBox");
                TextBox tb = (TextBox)Activator.CreateInstance(typField);
                tb.ID = FieldNames[i];
                if (!string.IsNullOrEmpty(q.FieldCSS))
                    tb.CssClass = FieldCSSs[i];
                
                pnlIn.Controls.Add(tb);

                if (!string.IsNullOrEmpty(q.FieldSuffix))
                {
                    Label lblSuffix = new Label();
                    lblSuffix.Text = "&nbsp;&nbsp;" + FieldSuffixes[i] + "&nbsp;&nbsp;";
                    pnlIn.Controls.Add(lblSuffix);
                }
                
            }

            if (!string.IsNullOrEmpty(q.FieldHelpText) && !string.IsNullOrEmpty(q.FieldHelpURL))
            {
                HtmlGenericControl aHelp = new HtmlGenericControl("a");
                aHelp.Attributes.Add("href", q.FieldHelpURL);
                aHelp.Attributes.Add("target", "_blank");
                aHelp.InnerText = q.FieldHelpText;
                pnlIn.Controls.Add(aHelp);
            }

            return pnlIn;
        }

        public Dictionary<Question, NameValueCollection> RetrieveQuestionValue(Question q, ContentPlaceHolder cph)
        {
            Dictionary<Question, NameValueCollection> dFieldValues = new Dictionary<Question, NameValueCollection>();
            NameValueCollection nvc = new NameValueCollection();

            List<string> FieldNames =  new List<string>(q.Name.Split('|'));

            for (int i = 0; i < FieldNames.Count; i++)
            {
                TextBox tb = (TextBox)cph.FindControl(FieldNames[i]);
                nvc.Add(FieldNames[i], tb.Text);              
            }

            dFieldValues.Add(q, nvc);

            return dFieldValues;
        }

        #endregion
    }
}